summaryrefslogtreecommitdiffstats
path: root/src/pages/projet/[slug].tsx
blob: f44d810a08386c8d7fefcabba364ea106c038a72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { getLayout } from '@components/Layouts/Layout';
import { CodeBlock, Gallery, Link, ResponsiveImage } from '@components/MDX';
import PostHeader from '@components/PostHeader/PostHeader';
import ProjectSummary from '@components/ProjectSummary/ProjectSummary';
import Sidebar from '@components/Sidebar/Sidebar';
import { Sharing, ToC } from '@components/Widgets';
import { config } from '@config/website';
import styles from '@styles/pages/Page.module.scss';
import {
  NextPageWithLayout,
  Project as ProjectData,
  ProjectProps,
} from '@ts/types/app';
import { loadTranslation } from '@utils/helpers/i18n';
import {
  getAllProjectsFilename,
  getProjectData,
} from '@utils/helpers/projects';
import { MDXComponents, NestedMDXComponents } from 'mdx/types';
import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
import Head from 'next/head';
import { useRouter } from 'next/router';
import { ParsedUrlQuery } from 'querystring';
import { ComponentType } from 'react';
import { Article, Graph, WebPage } from 'schema-dts';

const Project: NextPageWithLayout<ProjectProps> = ({
  project,
}: {
  project: ProjectData;
}) => {
  const router = useRouter();
  const projectUrl = `${config.url}${router.asPath}`;
  const { id, intro, meta, title, seo } = project;
  const dates = {
    publication: meta.publishedOn,
    update: meta.updatedOn,
  };

  const components: NestedMDXComponents = {
    Gallery: (props) => Gallery(props),
    Image: (props) => ResponsiveImage({ caption: props.caption, ...props }),
    Link: (props) => Link(props),
    pre: ({ children }) => CodeBlock(children.props),
  };

  const ProjectContent: ComponentType<MDXComponents> =
    require(`../../content/projects/${id}.mdx`).default;

  const webpageSchema: WebPage = {
    '@id': `${projectUrl}`,
    '@type': 'WebPage',
    breadcrumb: { '@id': `${config.url}/#breadcrumb` },
    name: seo.title,
    description: seo.description,
    inLanguage: config.locales.defaultLocale,
    reviewedBy: { '@id': `${config.url}/#branding` },
    url: `${config.url}`,
    isPartOf: {
      '@id': `${config.url}`,
    },
  };

  const publicationDate = new Date(dates.publication);
  const updateDate = new Date(dates.update);

  const articleSchema: Article = {
    '@id': `${config.url}/project`,
    '@type': 'Article',
    name: title,
    description: intro,
    author: { '@id': `${config.url}/#branding` },
    copyrightYear: publicationDate.getFullYear(),
    creator: { '@id': `${config.url}/#branding` },
    dateCreated: publicationDate.toISOString(),
    dateModified: updateDate.toISOString(),
    datePublished: publicationDate.toISOString(),
    editor: { '@id': `${config.url}/#branding` },
    thumbnailUrl: meta.hasCover ? `/projects/${id}.jpg` : '',
    image: meta.hasCover ? `/projects/${id}.jpg` : '',
    inLanguage: config.locales.defaultLocale,
    license: 'https://creativecommons.org/licenses/by-sa/4.0/deed.fr',
    mainEntityOfPage: { '@id': `${projectUrl}` },
  };

  const schemaJsonLd: Graph = {
    '@context': 'https://schema.org',
    '@graph': [webpageSchema, articleSchema],
  };

  return (
    <>
      <Head>
        <title>{seo.title}</title>
        <meta name="description" content={seo.description} />
        <meta property="og:url" content={`${projectUrl}`} />
        <meta property="og:type" content="article" />
        <meta property="og:title" content={title} />
        <meta property="og:description" content={intro} />
        <script
          type="application/ld+json"
          dangerouslySetInnerHTML={{ __html: JSON.stringify(schemaJsonLd) }}
        ></script>
      </Head>
      <article
        id="project"
        className={`${styles.article} ${styles['article--no-comments']}`}
      >
        <PostHeader title={title} intro={intro} meta={{ dates }} />
        <Sidebar position="left">
          <ToC />
        </Sidebar>
        <div className={styles.body}>
          <ProjectSummary id={id} title={title} meta={meta} />
          <ProjectContent components={components} />
        </div>
        <Sidebar position="right">
          <Sharing title={title} excerpt={intro} />
        </Sidebar>
      </article>
    </>
  );
};

Project.getLayout = getLayout;

interface ProjectParams extends ParsedUrlQuery {
  slug: string;
}

export const getStaticProps: GetStaticProps = async (
  context: GetStaticPropsContext
) => {
  const { locale } = context;
  const translation = await loadTranslation(locale);
  const { slug } = context.params as ProjectParams;
  const project = await getProjectData(slug);
  const breadcrumbTitle = project.title;

  return {
    props: {
      breadcrumbTitle,
      locale,
      project,
      translation,
    },
  };
};

export const getStaticPaths: GetStaticPaths = async () => {
  const filenames = getAllProjectsFilename();
  const paths = filenames.map((filename) => {
    return {
      params: {
        slug: filename,
      },
    };
  });

  return {
    paths,
    fallback: false,
  };
};

export default Project;